package game;

import log.Logger;
import opengl.program.GLProgramManager;

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;

public class Game3DEngine extends Game3DBase {
	public GLProgramManager glProgramManager;
	
	public static Game3DEngine getInstance() {
		if (instance == null) {
			instance = new Game3DEngine();
			instance.glProgramManager = GLProgramManager.get();
		}
		return instance;
	}

	@Override
	public void startUp() throws LWJGLException {
		try {
			super.startUp();
			this.initWorld();
			this.resized();
			this.loadWorld();
		} catch (Exception e) {
			this.shutDown();
		}
	}

	public void shutDown() {
	}

	@Override
	public void resized() {
		super.resized();
		this.resizedWorld();
	}

	protected void update() {
		CameraUpdater.update();
	}
	
	@Override
	public void render() {
		super.render();
		this.updateWorld();
		this.renderWorld();		
		this.exitOnGLError("loopCycle");
	}

	
	//------------------------------------------------------------------------
	protected void updateWorld() {
		while (Keyboard.next()) {
			if (!Keyboard.getEventKeyState()) {
				continue;
			}

			switch (Keyboard.getEventKey()) {
			case Keyboard.KEY_1:
				break;
			case Keyboard.KEY_2:
				Logger.info("key 2");
				break;
			}
		}
	}	

	public void renderWorld(){
		getGameService().render();
	}
	
	public void resizedWorld(){
		getGameService().resized();
	}
	public void initWorld() throws Exception{
		getGameService().init();
	}
	private void loadWorld() {
		getGameService().load();
	}

	private GameService getGameService(){
		return Game3DService.get();
		//return ToolStitcherService.get();
	}
	
	// ----------------------------------------------------------------private
	private static Game3DEngine instance;
	private Game3DEngine() {
	}
}
